package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeExpertCommitmentService;
import com.bidevalution.dao.entity.BeCommitmentTemplate;
import com.bidevalution.dao.entity.BeExpertCommitment;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.mapper.BeCommitmentTemplateMapper;
import com.bidevalution.dao.mapper.BeExpertCommitmentMapper;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.bidevalution.support.util.FileTypeConvertUtil;
import com.bidevalution.support.util.FileUtil;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by glodon on 2019-08-13.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BeExpertCommitmentServiceImpl implements BeExpertCommitmentService {

    @Value("${upload.file.path}")
    private String uploadFileUrl;
    @Autowired
    private BeSectionInfoMapper beSectionInfoMapper;

    @Autowired
    private BeCommitmentTemplateMapper beCommitmentTemplateMapper;

    @Autowired
    private BeExpertCommitmentMapper beExpertCommitmentMapper;

    /**
     * 根据模板生成专家承诺书pdf
     * @param sectionId
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public String convertCommitmentPdf(String sectionId, String loginName) throws Exception {
        BeSectionInfo beSectionInfo = beSectionInfoMapper.getSectionInfoByLoginName(sectionId, loginName);
        if(beSectionInfo == null) {
            throw new Exception("未查询到该专家评审的相关标段信息");
        }
        //查询专家承诺书，若承诺书已存在直接返回pdf结果
        BeExpertCommitment beExpertCommitment = beExpertCommitmentMapper.getExpertCommitment(sectionId, loginName);
        if(beExpertCommitment != null) {
            return beExpertCommitment.getCommintmentFileId();
        }

        //承诺书不存在，根据地区号获取专家承诺模板生成
        BeCommitmentTemplate beCommitmentTemplate = beCommitmentTemplateMapper.selectByAreaId("89");
        if(beCommitmentTemplate == null) {
            throw new Exception("该地区没有配置专家承诺模板");
        }
        String template = beCommitmentTemplate.getTemplate();
        if(template != null) {
            template = template.replace("{section_name}", beSectionInfo.getSectionName());
            template = template.replace("{agent_name}", beSectionInfo.getAgentName());
        }
        String pdfFilePath = "commitment.pdf";
        //html模板转换成pdf
        FileTypeConvertUtil.htmlText2pdf(template, pdfFilePath);
        String fileId = IdsUtil.uuid();
        //生成的pdf文件上传至文件服务器，并上传临时文件
        FileUtil.uploadFile(fileId, pdfFilePath, uploadFileUrl);
        //将结果存入数据库
        beExpertCommitment = new BeExpertCommitment();
        beExpertCommitment.setSeqId(IdsUtil.uuid());
        beExpertCommitment.setSectionId(sectionId);
        beExpertCommitment.setLoginName(loginName);
        beExpertCommitment.setCommintmentFileId(fileId);
        beExpertCommitment.setCreateTime(DateUtil.now());
        beExpertCommitment.setUpdateTime(DateUtil.now());
        if(beExpertCommitmentMapper.getExpertCommitment(sectionId, loginName) == null) {
            beExpertCommitmentMapper.insert(beExpertCommitment);
        }
        return fileId;
    }
}
